var showTrack, linkSearch;
//跳转到线路信息页面
var goLineInfo;
//跳转到车辆信息页面
var goCarInfo;
require(['grid', 'topBar', 'date'], function () {
	var $lineShiftDetailTab = $("#lineShiftDetailTab"),
		  mapDialog,
		  postData;
	//顶层搜索
	$('#top').topBar({
        left: [{
	            type: 'filter-more',
	            dateType: 'custom',
	            search: function () {
	            	searchLineShiftDetail();
	            },
	            item: [
	                {
	                    text: '时间快捷',
	                    name1: 'beginTime',
	                    name2: 'endTime',
	                    type: 'dateLine'
	                }
	            ]
	        }
        ]
    });
	
	$lineShiftDetailTab.grid({
		multiselect: false,
        url: "/lineShiftInfo/getDeviationDetail.htm",
        height:$(window).height()-80,
        colModel: [
			{name: "lineId", index: "lineId", label: '',  hidden: true},
            {name: "drvName", index: "drvName", label: '驾驶员', hidden: true},
            {name: "carId", label: "carId",hidden: true},
            {name: "licPltNo", index: "licPltNo", label: '车牌号',formatter: function (cellvalue,options,rowObject) {
            	return "<b><a href='javascript:void(0);' onclick='goCarInfo(\"" + rowObject.carId + "\")' class='text-link c-blue'>" + cellvalue + "</a></b>";
            }},
            {name: "carInCd", label: "车辆编号", width: 80},
            {name: "lineName", index: "lineName", label: '营运线路',formatter: 'customCurrency',formatoptions:{
                redbtn:true,
                css:'text-link c-blue',
                clickFn:function(rowObject,cellvalue){
                	goLineInfo(rowObject.lineId);
                }
            }},
            {name: "prvsStnName", index: "prvsStnName", label: '上一站点', width: 200},
            {name: "nextStnName", index: "nextStnName", label: '当前站点', width: 160},
            {name: "beginTime", index: "beginTime", label: '发生时间', width: 160},
            {name: "endTime", index: "endTime", label: '结束时间', width: 160},
            {
                name: "i3",
                index: "i3",
                label: '地图展示',
                width: 60,
                align: "center",
                formatter: function (cellvalue, options, rowObject) {
                    return '<b><a onclick="showTrack(\'{0}\',\'{1}\',\'{2}\',\'{3}\',\'{4}\',\'{5}\')" class="operate operate-scan" >查看</a></b>'.format(rowObject.cityName, rowObject.lineName, rowObject.carId, rowObject.beginTime, rowObject.endTime, rowObject.provinceName);
                }
            }
        ]
	});
	
	/**
     * 线路偏移对话框
     */
    function getDialog(option) {
        option = option || {};
        if (mapDialog) {
        	mapDialog.dialog('open');
            if (option.title) {
            	mapDialog.dialog('setOption', 'title', option.title);
            }
            return mapDialog;
        } else {
        	mapDialog = $('<div>').dialog({
                title: option.title || '线路偏移明细',
                url: './service/report/reportLineShiftDetailMap.htm',
                height: 570,
                width: 1050,
                autoOpen: true,
                close: function () {
                    $(this).dialog('exec', 'clearMap');//reset为窗体中的方法
                }
            });
        }
    }
    
    
    /**
	 * 获取左侧选择的节点信息
	 */
    function getPostData() {
        var rcx = commonFun.getTreeCheckNodeToArgs();
        if (rcx) {
            postData = $.extend(rcx, $("#top").parent().getValues());
        }
        return rcx;
    }
    
    /**
     * 查询
     */
	function searchLineShiftDetail(){
		if(getPostData()){
			$lineShiftDetailTab.grid('setGridParam', {
                page: 1,
                datatype: "json",
                postData: postData
            }).trigger("reloadGrid");
		}
	}
	
	/**
	 * 跳转查询
	 */
	linkSearch = function(carId, beginTime){
        var endTime = beginTime + " 23:59:59";
        $lineShiftDetailTab.grid('setGridParam', {
            page: 1,
            datatype: "json",
            postData: {
            	"carIds":carId,
            	"beginTime":beginTime,
            	"endTime":endTime
            }
        }).trigger("reloadGrid");
	};
	
	 //跳转到车辆信息页面
    goCarInfo = function(carId){
    	var data = {};
    	data.carId = carId;
    	$.openTab('6bad880fef164ec6adaea7b18420eb6e','showCarInfo',[data]);
    }
    //跳转到线路信息页面
    goLineInfo = function(lineId){
    	var data = {};
    	data.lineId = lineId;
    	$.openTab('4db17f8a322b4749a61eb2f8a12023ae','showLineInfo',[data]);
    }
	
	//点击查看事件（显示弹出框、加载线路、加载偏移轨迹）
	showTrack = function(city, lineName, carId, beginTime, endTime,provinceName){
    	if(provinceName == '北京'||provinceName == '上海'||
                provinceName == '天津'||provinceName == '重庆'){
    		city = provinceName;
    	}
    	//弹出层打开
    	getDialog({title: '线路偏移明细地图'});
    	getDialog().dialog('exec', 'loadLine', city, lineName, carId);
        getDialog().dialog('exec', 'loadTrack', carId, beginTime, endTime);
    }
});